package 线程池;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class 线程池的底层实现原理 {
    public static void main(String[] args) throws Exception{
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
                                2,  //核心线程数
                                5,  //最大线程数 核心线程+临时线程
                                1,     //线程的空闲时间，对临时线程没有影响，临时线程执行完毕后立马销毁
                                                    // 指的是配置了poolExecutor.allowCoreThreadTimeOut(ture)后核心线程空闲规定时间后销毁
                                TimeUnit.SECONDS,   //空闲时间单位
                                new ArrayBlockingQueue<>(10), //等待队列中允许存放10个线程
                                Executors.defaultThreadFactory(),   //线程的创建者  固定写法
                                new ThreadPoolExecutor.AbortPolicy() //拒绝策略，允许同时运行5个线程，10个排队，第16个之后拒绝加入，直接报错

                );
        poolExecutor.submit(() -> {
            System.out.println(Thread.currentThread().getName());
        });
        poolExecutor.submit(() -> {
            System.out.println(Thread.currentThread().getName());
        });
        poolExecutor.submit(() -> {
            System.out.println(Thread.currentThread().getName());
        });

        Thread.sleep(3000);
        System.out.println(poolExecutor.getPoolSize());
    }
}

// ctrl alt shift +u
